package com.iflytek.aidemo.teacher;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.iflytek.aidemo.R;

import java.util.List;

public class StudentScoreAdapter extends RecyclerView.Adapter<StudentScoreAdapter.VH> {

    public interface OnStudentClickListener {
        void onStudentClick(StudentScoreItem item);
    }

    private final List<StudentScoreItem> data;
    private final OnStudentClickListener listener;

    public StudentScoreAdapter(List<StudentScoreItem> data, OnStudentClickListener listener) {
        this.data = data;
        this.listener = listener;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_student_score, parent, false);
        return new VH(v);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        StudentScoreItem item = data.get(position);
        holder.tvName.setText(item.getStudentName());
        holder.tvScore.setText(String.format("%.1f", item.getLatestScore()));
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) listener.onStudentClick(item);
        });
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    static class VH extends RecyclerView.ViewHolder {
        TextView tvName;
        TextView tvScore;
        VH(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_student_name);
            tvScore = itemView.findViewById(R.id.tv_student_score);
        }
    }
}
